package com.rjj.wlqq.listen.aop;

import com.rjj.wlqq.tool.console.ConsoleTool;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

/**
 * @author renjianjun
 * @date 2020/9/7 21:30
 * @email 215060580@qq.com
 * <p>
 * 放弃aop，因为需要配置路径，不同的项目需要配置不同路径麻烦，直接使用拦截器方便
 *
 * 这里是配置了一个固定的路径下的请求会使用这个，测试，现在放弃。
 */
@Slf4j
//@Component
//@Aspect
public class AopRequestMapping {

    @Pointcut(value = "execution(public * com.rjj.wlqq.index.*.*(..))")
    public void aop() {
    }

    @Around(value = "aop()")
    public void around(ProceedingJoinPoint point) {
        try {
            point.proceed();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();

            MethodSignature signature = (MethodSignature) point.getSignature();

            //pathTest
            String signatureName = signature.getName();
            int modifiers = signature.getModifiers();
            System.out.println(modifiers + "xxxxx");
            //com.rjj.wlqq.index.IndexController
            String declaringTypeName = signature.getDeclaringTypeName();
            //com.rjj.wlqq.index.IndexController
            String clazz = point.getTarget().getClass().getName();
            //GET
            String requestMethod = request.getMethod();
            //访问路径 /pathTest
            String requestServletPath = request.getServletPath();

            //public void com.rjj.wlqq.index.IndexController.pathTest(javax.servlet.http.HttpServletRequest) throws java.io.FileNotFoundException
            Method method = signature.getMethod();
            //http://127.0.0.1:8080/pathTest
            String url = request.getRequestURL().toString();
            //127.0.0.1
            String remoteAddr = request.getRemoteAddr();

            //point.getTarget().getClass().getSingleName()
            String classStr = clazz.substring(clazz.lastIndexOf(".") + 1);

            ConsoleTool.Console(request, signatureName, clazz, requestMethod, requestServletPath, classStr);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

}
